package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.signature.common.mapper.CertInfoMapper;
import com.credithc.signature.common.po.SealApplyInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年07⽉08⽇<br/>
 * @version：v1.0.0<br>
 */
@RestController
@RequestMapping("/manager/scs/v2")
@Slf4j
public class IDNmberController {

    @Resource(name = "masterJdbcTemplate")
    JdbcTemplate masterJdbcTemplate;

    @Resource(name = "salveJdbcTemplate")
    JdbcTemplate salveJdbcTemplate;

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    private CertInfoMapper certInfoMapper;

    /**
     * 删除冲突的数据
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/conflict/{userIdNo}")
    @ResponseBody
    public Message resolution(@PathVariable("userIdNo") String userIdNo) throws Exception {
        try {
            // 1、首先根据userIdNo查出中金SealApplyInfo表中的数据,得到sealCode
            String idNo = "'" + userIdNo + "'";
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.append("SELECT * FROM SEAL_APPLY_INFO WHERE IDENTIFICATION_NO = ").append(idNo);
            List<SealApplyInfoDTO> dtos = salveJdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(SealApplyInfoDTO.class));

            // 2、根据sealCode删除中金seal表中的数据
            if (CollectionUtils.isNotEmpty(dtos)) {
                for (SealApplyInfoDTO dto : dtos) {
                    StringBuilder sqlBuilder2 = new StringBuilder();
                    String sealCode = "'" + dto.getSealCode() + "'";
                    sqlBuilder2.append("DELETE FROM SEAL WHERE CODE = ").append(sealCode);
                    masterJdbcTemplate.update(sqlBuilder2.toString());
                }
            }

            // 3、根据userIdNo删除中金SealApplyInfo表中的数据
            StringBuilder sqlBuilder3 = new StringBuilder();
            sqlBuilder3.append("DELETE FROM SEAL_APPLY_INFO WHERE IDENTIFICATION_NO = ").append(idNo);
            masterJdbcTemplate.update(sqlBuilder3.toString());

            // 4、根据userIdNo删除签章SealApplyInfo表中的数据
            Example example = new Example(SealApplyInfoDTO.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("identificationNo", userIdNo);
            certInfoMapper.deleteByExample(example);

            // 5、返回结果
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, "删除IdNumber冲突数据成功");
        } catch (Exception e) {
            log.error("删除IdNumber冲突数据时发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "删除IdNumber冲突数据时发生异常");
        }
    }
}
